package domein;

import java.io.IOException;
import java.util.*;

// GET / HTTP/1.1
// Host: 145.89.125.196:12355
// User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.18 (KHTML, like Gecko) Version/4.0.1 Safari/530.18
// Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
// Accept-Language: en-us
// Accept-Encoding: gzip, deflate
// Connection: keep-alive

public class Request extends HashMap {
    String req = "";
	private String contentBase;
	
    public Request(SocketInputStream sis,String contentBase) throws IOException
    {
    	this.contentBase = contentBase;
        String line = "";
        while((line = sis.readLine()) != null){
            req += line+"\n";
            // METHOD
            if(line.substring(0,3).equals("GET")){
                this.put("method", line.split(" ", 3)[0]);
                this.put( "url", contentBase+"\\"+line.split(" ", 3)[1].substring(1));
                System.out.println("ContentBase:" + contentBase);
                System.out.println(this.get("method"));
                System.out.println(this.get("url"));
            }
            // AGENT
            if( line.substring(0,11).equals("User-Agent:") ){
                this.put("agent", line.substring(12));
            }
        }
	}

	public String getMETHOD(){
		return (String) this.get("method");
	}

    public String getRequestURI() {
        return (String) this.get("url");
    }

    public String getVersion(){
        return (String) this.get("agent");
    }
    public String toString(){
        return req;
    }

	public void setContentBase(String contentBase) {
		this.contentBase = contentBase;
		System.out.println("setContentBase: "+this.contentBase);
	}

}
